﻿using System;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using Microsoft.Phone.Controls;

namespace WinPhoneClient
{
    public class SizeConverter : IValueConverter
    {
        private const double ActualWidth = 470;

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return (double) value*ActualWidth;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class ColorConverter : IValueConverter
    {
        private readonly Brush FreeBrush = new SolidColorBrush(Colors.Green);
        private readonly Brush BusyBrush = new SolidColorBrush(Colors.Red);

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return (bool) value ?  FreeBrush : BusyBrush;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class MapItemsControl : ItemsControl
    {
        protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
        {
            
            FrameworkElement contentitem = element as FrameworkElement;
            Binding leftBinding = new Binding("X") { Converter = new SizeConverter() };
            Binding topBinding = new Binding("Y") { Converter = new SizeConverter() };
            contentitem.SetBinding(Canvas.LeftProperty, leftBinding);
            contentitem.SetBinding(Canvas.TopProperty, topBinding);
            base.PrepareContainerForItemOverride(element, item);
        }
    }

    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
        }

        private void OnLiveViewClick(object sender, EventArgs e)
        {
            var frame = Parent as Frame;
            if (frame != null)
            {
                frame.Navigate(new Uri("/LiveViewPage.xaml", UriKind.Relative));
            }
        }
    }
}
